package com.cdl.library.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.cdl.library.R;

public class CircularProgressBar extends View {

    private Paint progressPaint, textPaint;
    private RectF rectF;
    private float progress = 0;
    private int backgroundColor;
    private int progressColor;

    public CircularProgressBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CircularProgressBar, 0, 0);
        progress = typedArray.getFloat(R.styleable.CircularProgressBar_progress_num, 0);
        backgroundColor = typedArray.getColor(R.styleable.CircularProgressBar_backgroundColor, 0xFFDDDDDD);
        progressColor = typedArray.getColor(R.styleable.CircularProgressBar_progress_color, 0xFFFF0000);
        typedArray.recycle();
        initPaint();
    }

    private void initPaint() {
        progressPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        progressPaint.setColor(progressColor);
//        progressPaint.setStrokeCap(Paint.Cap.ROUND);
        progressPaint.setStyle(Paint.Style.STROKE);

        rectF = new RectF();
        textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setColor(progressColor);
        textPaint.setTextAlign(Paint.Align.CENTER);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getWidth();
        int height = getHeight();
        int strokeWidth = width / 20;
        int size = Math.min(width, height);
        float halfStrokeWidth = strokeWidth / 2;
        rectF.set(halfStrokeWidth, halfStrokeWidth, size - halfStrokeWidth, size - halfStrokeWidth);
        progressPaint.setStrokeWidth(strokeWidth);
        progressPaint.setColor(backgroundColor);
        canvas.drawOval(rectF, progressPaint);

        progressPaint.setColor(progressColor);
        float angle = 360 * progress / 100;
        canvas.drawArc(rectF, -90, angle, false, progressPaint);

        drawTextByProgress(progress, canvas, width, height);
    }

    private void drawTextByProgress(float progress, Canvas canvas, int width, int height) {
        String progressText = (int) progress + "";
        if (progress < 1) {
            progressText = "...";
        }
        float xPos = width / 2;
        float yPos = (height / 2) - ((textPaint.descent() + textPaint.ascent()) / 2);
        textPaint.setTextSize(width / 4);
        canvas.drawText(progressText, xPos, yPos, textPaint);
    }

    public void setProgress(float progress) {
        this.progress = progress;
        invalidate();
    }

    public float getProgress() {
        return progress;
    }
}
